import 'package:flutter/material.dart';
import 'package:shenling_project/message/MessagePage.dart';
import 'package:shenling_project/home/TaskPage.dart';
import 'package:shenling_project/mine/MinePage.dart';
import 'package:shenling_project/utils/eventBus.dart';
import 'package:shenling_project/utils/tokenManager.dart';

class ShenLingIndex extends StatefulWidget {
  const ShenLingIndex({super.key});

  @override
  State<ShenLingIndex> createState() => _ShenLingIndexState();
}

class _ShenLingIndexState extends State<ShenLingIndex>
    with SingleTickerProviderStateMixin {
  int _currentIndex = 0;
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _initToken();

    // 创建与Tab数量对应的控制器
    _tabController = TabController(length: 3, vsync: this);
    // 添加状态变化监听
    _tabController.addListener(_handleTabSelection);

    event.on<LogoutGetData>().listen((e) {
      Navigator.pushNamed(context, '/login');
    });
  }

//初始化首选项
  _initToken() async {
    await TokenManager().init();
  }

  // 处理Tab选择变化
  void _handleTabSelection() {
    if (_tabController.indexIsChanging) {
      setState(() {
        _currentIndex = _tabController.index;
      });
    }
  }

  // 根据索引获取图标颜色
  Color _getIconColor(int index) {
    return _currentIndex == index ? Colors.red : Colors.black;
  }

  @override
  Widget build(BuildContext context) {
    return
        // MaterialApp(
        //   home: DefaultTabController(
        //     length: 3,
        //     child:
        Scaffold(
      // appBar: AppBar(
      //   title: const Text('神领物流'),
      // ),
      body: TabBarView(
          controller: _tabController,
          //禁止滑动
          physics: const NeverScrollableScrollPhysics(),
          children: const [TaskPage(), Messagepage(), Minepage()]),
      bottomNavigationBar: TabBar(
        controller: _tabController,
        labelColor: Colors.red,
        unselectedLabelColor: Colors.black,
        //选中之后icon的颜色
        tabs: [
          Tab(
              icon: Image.asset(
                'assets/images/task.png',
                width: 24,
                height: 24,
                color: _getIconColor(0),
              ),
              text: '任务'),
          Tab(
              icon: Image.asset(
                'assets/images/mess.png',
                width: 24,
                height: 24,
                color: _getIconColor(1),
              ),
              text: '消息'),
          Tab(
              icon: Image.asset(
                'assets/images/mine.png',
                width: 24,
                height: 24,
                color: _getIconColor(2),
              ),
              text: '我的'),
        ],
        indicatorColor: Colors.transparent, // 隐藏指示器
      ),
      // );
      // ),
    );
  }
}
